;;(load "yexp.scm")
;;(load "key.scm")
;;(load "macro-aux.scm")

(def/err (cyl-macro s n macro-str . macro-l)
  (let* ((res-str "")
	 (new-key "")
	 (res-cyl '())
	 (res-st #f)
	 (res-deli-str "")
	 (res-deli-st #f)
	 (st #f)
	 (deli-str "")
	 (define-func-name/flag #f)
	 (cyl-l '())
	 (cb-cyl-l '())
	 (fd-cyl-l '())
	 (mc-cyl-l '())
	 (str-l '())
	 (cb-str-l '())
	 (fd-str-l '())
	 (mc-str-l '())
	 (mc-cyl-l '())
	 (mc-deli-st #f)
	 (last-str "")
	 (last-cyl '())
	 (last-st #f)
	 (last-fn "")
	 (last-par-num 0)
	 (macro-coding-l (find-dict-coding-list macro-str 'system))
	 (macro-coding (nth 1 macro-coding-l))
	 (macro-type (nth 0 macro-coding-l))
	 (deli-cyl '())
	 (key-travel2-list '("针对每个索引" "针对每个元素" "针对每个子项" "针对每组索引子项"))
	 (key-symbol-list '("别名" ))
	 )
    (pre-handle-macro/macro-aux)
    (tr-string (loc c s n)
	       ;;(y-or-n "CYL-tr-string:c=~a,loc=~a,mc-cyl-l=~a.\n" c loc mc-cyl-l)
	       (cond
		((rd-p-deli c)
		 (let-set! (deli-str loc st) (get-deli s loc loc '开始))
		 ;;(y-or-n "macro1:loc=~a,deli-str=~a,st=~a\n" loc deli-str st)
		 (cond
		  ((and (eq? st '函数定义分隔符)(eq? cyl-define-status 'func-define))
		   (run/err rewrite-cyl/pure-yexp->syntax mc-cyl-l)
		   (if (string? cyl-adverb-value)
		       (let ((cyl-tl '())(str-tl '()))
			 (push (pop mc-cyl-l) cyl-tl)
			 (push (pop mc-str-l) str-tl)
			 (push (list deli-str (string-length deli-str) deli-std-coding '分隔符 (cyl-serial)) cyl-tl)
			 (push deli-str str-tl)
			 (handle-macro/macro-aux cyl-tl str-tl)
			 )
		       (begin
			 (push (list deli-str (string-length deli-str) deli-std-coding '分隔符 (cyl-serial)) mc-cyl-l)
			 (push deli-str mc-str-l)
			 ))
		   )
		  ((eq? st '函数定义分隔符)
		   (exit-cyl-macro (list 'err loc (merge-num loc last-cyl 1) '非法语法 (err-format 'cyl-macro loc "函数定义分隔符不应该出现在cyl-macro中，只允许在cyl中被处理。")))
		   )
		  ((eq? st '结束分隔符)
		   ;;(y-or-n "MACRO-stop-deli1:mc-cyl-l=~a." mc-cyl-l)
		   (run/err rewrite-cyl/pure-yexp->syntax mc-cyl-l)
		   (set! deli-cyl (list deli-str (string-length deli-str) deli-return-coding '结束分隔符 (cyl-serial)))
		   ;;(push deli-str mc-str-l)
		   ;;(run/err make-macro-cyl-list/aux)
		   ;;(set! cyl-define-status 'top-level)
		   ;;(y-or-n "MACRO-stop-deli2:mc-cyl-l=~a,loc=~a,status=~a." mc-cyl-l loc cyl-define-status)
		   (exit-cyl-macro (list (merge-str loc mc-str-l) loc (merge-cyl loc mc-cyl-l '系统宏调用) '系统宏调用 deli-cyl))
		   )
		  ((eq? st '语句终结分隔符)
		   (run/err rewrite-cyl/pure-yexp->syntax mc-cyl-l)
		   (run/err make-macro-cyl-list/aux)
		   (set! deli-cyl (list deli-str (string-length deli-str) deli-return-coding '语句终结分隔符 (cyl-serial)))
		   ;;(push deli-str mc-str-l)
		   (exit-cyl-macro (list (merge-str loc mc-str-l) loc (merge-cyl loc mc-cyl-l '系统宏调用) '系统宏调用 deli-cyl))
		   )
		  ((eq? st '分隔符)
		   (run/err rewrite-cyl/pure-yexp->syntax mc-cyl-l)
		   (if (string? cyl-adverb-value)
		       (let ((cyl-tl '())(str-tl '()))
			 (push (pop mc-cyl-l) cyl-tl)
			 (push (pop mc-str-l) str-tl)
			 (push (list deli-str (string-length deli-str) deli-std-coding '分隔符 (cyl-serial)) cyl-tl)
			 (push deli-str str-tl)
			 (handle-macro/macro-aux cyl-tl str-tl)
			 )
		       (begin
			 (push (list deli-str (string-length deli-str) deli-std-coding '分隔符 (cyl-serial)) mc-cyl-l)
			 (push deli-str mc-str-l)
			 ))
		   )
		(else
		 ;;maybe not get here.
		 (handle-error s (list 'err loc (merge-num loc last-cyl 1) '非法语法 (err-format 'cyl-macro loc "[~a]非法分隔符。" deli-str)))
		   )
		  ))
		(else
		 ;;(y-or-n "CYL-macro1:loc=~a\n" loc)
		 (let-set!  (last-str loc ls rs last-cyl last-st last-fn last-par-num) (run/err yexp s loc 0 0))
		 ;;(y-or-n "CYL-macro2:loc=~a,staus=~a,last-cyl=~a,macro-list=~a,last-st=~a.\n" loc cyl-define-status last-cyl cyl-current-macro last-st)
		 (cond
		  ((eq? last-st '系统宏)
		   (handle-macro/macro-aux mc-cyl-l mc-str-l)
		   ;;(y-or-n "macro-key:loc=~a\n" loc)
		   )
		  ((eq? last-st '系统关键字)
		   (if (member last-str key-travel2-list) (set! cyl-key-travel2 #t) (if (member last-str key-symbol-list) (set! cyl-key-symbol #t)))
		   (if (string=? last-str "执行") (set! cyl-key-travel2 #f))
		   ;;(y-or-n "cyl-macro:last-str=~a,cyl-key-travel2=~a,key-travel2-list=~a.\n" last-str cyl-key-travel2 key-travel2-list)
		   (handle-key/macro-aux)
		   ;;(y-or-n "macro-macro:loc=~a\n" loc)
		   )
		  (else
		   (push last-cyl mc-cyl-l)
		   (push last-str mc-str-l))))
		))
    ;;handle after string traveling.
    
    (let ((loc (string-length s))
	  (cur-str "")
	  (cur-st (list "\n" 1 '(sys 11) '结束分隔符 (cyl-serial)))
	  )
      ;;(y-or-n "CYL_END:cyl-l=~a" cyl-l)
      (list (merge-str loc mc-str-l) loc (merge-cyl loc mc-cyl-l '系统宏调用) '系统宏调用 cur-st))))
      
	
	
		  
		     
		     
		   
		   
		   
		   
			 
		 
		 
   
